import store from '@/store'
import router from '@/router'

// 判断是否有权限
export function checkPermission(value) {
	if (!$config.needFeatchMenu) return true
	const perms = store.getters.btnRoleList[router.history.current.name]
	return Array.isArray(perms) && perms.includes(value)
}

// 校验用户是否拥有按钮权限
function hasPermission(el, binding) {
	try {
		// 有权限直接返回
		if (checkPermission(binding.value)) return
		// 没有权限移除节点
		el.parentNode && el.parentNode.removeChild(el)
	} catch (error) {
		throw new Error('need perms! Like v-has-permission="\'add\'"')
	}
}

export default {
	inserted(el, binding) {
		hasPermission(el, binding)
	}
	// update(el, binding) {
	//   hasPermission(el, binding)
	// }
}
